package com.yunhe.spring.controller;

import com.yunhe.spring.entity.Student;
import com.yunhe.spring.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class StudentController {

        @Autowired
        private StudentService studentService;

    @RequestMapping("/")
    public String index(){
        return "index";
    }


    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(HttpSession session){
        List<Student> all = studentService.findAll();
      session.setAttribute("studentlist" ,all);

        return "/list";
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/save",method = RequestMethod.GET)
    public String save(){
        return "/add";
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(Student student){
        studentService.save(student);
        return "/list";
    }

    @RequestMapping(value = "/delete")
    public String remove( Integer studentId,HttpSession session){
        studentService.remove(studentId);
        List<Student> all = studentService.findAll();
        session.setAttribute("studentlist" ,all);
        return "/list";
    }
}
